//
//  AppDelegate.m
//  RunningMan
//
//  Created by dllo on 16/5/17.
//  Copyright © 2016年 WSJ. All rights reserved.
//

#import "AppDelegate.h"
#import "WSJLTabBarController.h"
#import "JGTGuideViewController.h"
#import "WSJLeftViewController.h"
#import <JPush/JPUSHService.h>

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    WSJLTabBarController *tab = [[WSJLTabBarController alloc] init];
    tab.tabBar.barTintColor = [UIColor colorWithRed:88/255.0 green:157/255.0 blue:168/255.0 alpha:1];
    tab.tabBar.tintColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    WSJLeftViewController *left = [[WSJLeftViewController alloc]init];
    self.LeftSlideVC = [[DeckViewController alloc]initWithLeftView:left andMainView:tab];
    
    JGTGuideViewController *jgt = [[JGTGuideViewController alloc]init];
    NSUserDefaults *defaults = USERDEFAULTS;
    if (![defaults boolForKey:@"notFirst"]) {
        self.window.rootViewController = jgt;
        void(^block)(void) = ^(void){
            self.window.rootViewController = self.LeftSlideVC;
            
            
        };
        jgt.block = block;
    }else{
        self.window.rootViewController = self.LeftSlideVC;
        
    }
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
        //可以添加自定义categories
        [JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |
                                                          UIUserNotificationTypeSound |
                                                          UIUserNotificationTypeAlert)
                                              categories:nil];
    } else {
        //categories 必须为nil
        [JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
                                                          UIRemoteNotificationTypeSound |
                                                          UIRemoteNotificationTypeAlert)
                                              categories:nil];
    }
    
    //如不需要使用IDFA，advertisingIdentifier 可为nil
    [JPUSHService setupWithOption:launchOptions appKey:appKey
                          channel:channel
                 apsForProduction:isProduction
            advertisingIdentifier:nil];
         return YES;
}
//注册成功返回token的方法
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
    NSLog(@"deviceToken:%@",deviceToken);
    //拿到deicetoken 上传给服务器
    [JPUSHService registerDeviceToken:deviceToken];
    
}
//接收远程通知的方法(当程序处在前台运行状态,推送过来的消息会自动触发此方法,如果程序处在后台或者没有运行,需点击推送的提示或者系统通知中心的提示才会触发此方法,直接点击App图标不会触发)
//userInfo:推送过来的消息详情
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
    NSLog(@"userInfo:%@",userInfo);
    //根据userInfo里的信息做对应的操作
    //处理角标;
    application.applicationIconBadgeNumber = 0;
    [JPUSHService setBadge:0];
    [JPUSHService handleRemoteNotification:userInfo];
}

//注册通知失败
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{
    //根据error做不同的处理操作
    
    NSLog(@"error:%@",error);
    
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
